package com.example.myapplication.tack_21;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.example.myapplication.R;
import com.example.myapplication.tack_21.adapter.DectorAdapter;
import com.example.myapplication.tack_21.data.DectorData;
import com.example.myapplication.utils.Common;
import com.example.myapplication.utils.OkHttpTool;

import java.util.List;

public class DectorActivity extends AppCompatActivity {
    private ImageView exit;
    private RecyclerView dector;
    private List<DectorData.RowsDTO> listDector;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tack_21_activity_dertoc);
        Intent intent = getIntent();
        String id = intent.getStringExtra(Common.COMMON);
        init();
        getDector(id);
        exit_();

    }

    private void exit_() {
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DectorActivity.this.finish();
            }
        });
    }

    private void getDector(String id) {
        String url = "prod-api/api/pet-hospital/pet-doctor/list?typeId="+id;
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, DectorData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                DectorData data = (DectorData)obj;
                if(data.getCode() == 200){
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            listDector = data.getRows();
                            DectorAdapter();
                        }
                    });
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void DectorAdapter() {
        DectorAdapter dectorAdapter = new DectorAdapter(listDector, DectorActivity.this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(DectorActivity.this);
        dector.setLayoutManager(linearLayoutManager);
        dector.setAdapter(dectorAdapter);
    }

    private void init() {
        exit = findViewById(R.id.exit);
        dector = findViewById(R.id.dec_tor);
    }
}